package com.sas.entity.converter;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

/**
 * 员工状态字符串与整型转换器。
 * DB: 'ACTIVE' | 'INACTIVE'
 * Entity: 1 | 0
 */
@Converter(autoApply = false)
public class EmployeeStatusConverter implements AttributeConverter<Integer, String> {

    @Override
    public String convertToDatabaseColumn(Integer attribute) {
        if (attribute == null) return null;
        switch (attribute) {
            case 1:
                return "ACTIVE";
            case 0:
                return "INACTIVE";
            default:
                return "INACTIVE";
        }
    }

    @Override
    public Integer convertToEntityAttribute(String dbData) {
        if (dbData == null) return null;
        switch (dbData) {
            case "ACTIVE":
                return 1;
            case "INACTIVE":
                return 0;
            default:
                try {
                    return Integer.parseInt(dbData);
                } catch (NumberFormatException ex) {
                    return 0;
                }
        }
    }
}